package cn.jetpiece.cloud.joss.app.config;

import cn.jetpiece.cloud.joss.app.core.constant.OssConstants;
import cn.jetpiece.cloud.resource.config.BaseSwaggerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XuJZ
 * @since 2020/9/5
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket objectApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(BaseSwaggerConfig.apiInfo())
                .groupName("对象存储")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.jetpiece.cloud.joss.app.modular.object"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters());
    }

    @Bean
    public Docket basicApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(BaseSwaggerConfig.apiInfo())
                .groupName("基础服务")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.jetpiece.cloud.joss.app.modular.basics"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters());
    }

    @Bean
    public Docket accessorApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(BaseSwaggerConfig.apiInfo())
                .groupName("接入授权")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.jetpiece.cloud.joss.app.modular.accessor"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters());
    }

    private List<Parameter> parameters() {
        Parameter accessKey = new ParameterBuilder()
                .name(OssConstants.ACCESS_KEY)
                .description("公钥")
                .modelRef(new ModelRef("string"))
                .parameterType("query")
                .required(true)
                .build();
        Parameter sign = new ParameterBuilder()
                .name(OssConstants.SIGN)
                .description("签名参数，MD5(密钥+公钥+时间戳+密钥)")
                .modelRef(new ModelRef("string"))
                .parameterType("query")
                .required(true)
                .build();
        Parameter timestamp = new ParameterBuilder()
                .name(OssConstants.TIMESTAMP)
                .description("时间戳,精确到毫秒")
                .modelRef(new ModelRef("string"))
                .parameterType("query")
                .required(true)
                .build();

        List<Parameter> parameters = new ArrayList<>();
        parameters.add(accessKey);
        parameters.add(sign);
        parameters.add(timestamp);
        return parameters;
    }
}
